package 代码随想录.回溯.a;

import java.util.ArrayList;
import java.util.List;

public class a77组合 {
    static class Solution {
        public static List<List<Integer>> ans;
        public static List<Integer> cur;

        public List<List<Integer>> combine(int n, int k) {
            ans = new ArrayList<>();
            cur = new ArrayList<>();
            int index = 1;
            backtracking(n, k, index);
            return ans;
        }

        private void backtracking(int n, int k, int index) {
            if (cur.size() == k) {
                ans.add(new ArrayList<>(cur));
                return;
            }
            // 1 2 3 4
            for (int i = index; cur.size() + n - i + 1 >= k; i++) {
                cur.add(i);
                backtracking(n, k, i + 1);
                cur.remove(cur.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int n = 4;
        int k = 2;
        List<List<Integer>> combine = solution.combine(n, k);
        for (List<Integer> integers : combine) {
            System.out.println(integers);
        }
    }
}
